/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*

 This file is part of the qtscreenshot project
 author: Steven Bai<baizhenxuan#gmail.com>
 with full test under Ubuntu 10.04

 */
#include "screenshot.h"

ScreenShot::ScreenShot(QWidget *parent)
	: QWidget(parent)
{
    draw = DrawInit;
    this->setWindowState(Qt::WindowActive|Qt::WindowFullScreen);
    this->setWindowFlags(Qt::WindowStaysOnTopHint|Qt::FramelessWindowHint|Qt::Window);
    setCursor(Qt::CrossCursor);
    fullScreenImage =QPixmap::grabWindow(QApplication::desktop()->winId());
        this->grabMouse (Qt::CrossCursor);
}

void ScreenShot::showEvent(QShowEvent *event)
{
   // this->setWindowState(Qt::WindowActive|Qt::WindowFullScreen);
}

void ScreenShot::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);
    //QPalette  p=this->palette ();
    //p.setCurrentColorGroup (QPalette::Disabled);
   // this->setPalette (p);
    switch(draw)
    {
    case DrawResult:
        painter.drawPixmap (0,0,fullScreenImage);
        painter.drawRect (QRect(startPoint,endPoint));
        painter.drawText (snapRect,trUtf8("已经将所选区域复制到剪贴板,按Esｃ退出"));
        break;
    case DrawRect:
        painter.drawPixmap (0,0,fullScreenImage);
        painter.drawRect (QRect(startPoint,endPoint));
        break;
    case DrawInit:
        painter.drawPixmap (0,0,fullScreenImage);
        break;
    }

}

void ScreenShot::mouseMoveEvent(QMouseEvent *event)
{
	QPoint pt = event->pos();
    if (draw==DrawRect)
	{
        endPoint = event->pos();
        update();
	}
	else
	{
		if (snapRect.contains(pt.x(), pt.y()))
		{
			setCursor(Qt::SizeAllCursor);
		}
		else
		{
			setCursor(Qt::CrossCursor);
		}
	}
}

void ScreenShot::mousePressEvent(QMouseEvent *event)
{
    if( event->button () == Qt::LeftButton && draw==DrawResult)
        close();
    if (event->button() == Qt::LeftButton)
	{
        draw = DrawRect;
		startPoint = event->pos();
        //update();
	}
}

void ScreenShot::mouseReleaseEvent(QMouseEvent *event)
{
	if (event->button() == Qt::LeftButton)
	{
        draw = DrawResult;
        endPoint = event->pos();
        snapRect = QRect(startPoint, endPoint);
        update();
        QPixmap ImgRegionSelect = this->fullScreenImage.copy(snapRect);
        qApp->clipboard ()->setPixmap (ImgRegionSelect);
	}
}



void ScreenShot::paint(QPixmap &pixmap)
{
	QPainter painter(&pixmap);
    painter.drawRect(QRect(startPoint, endPoint));
	update();
}

void ScreenShot::keyPressEvent(QKeyEvent *event)
{
	if (event->key() == Qt::Key_Escape)
	{
		close();
	}
	else if (event->key() == Qt::Key_Space)
	{
		if (!snapRect.isNull())
		{
            copy();
		}
	}
}

void ScreenShot::save()
{
	QString filename;
    this->releaseMouse ();
	filename = QFileDialog::getSaveFileName(this, tr("保存图像"),
		QDir::currentPath(), tr("Image (*.jpg *.png *.bmp)"));
	if (filename.isNull())
	{
		return;
	}
    QPixmap ImgRegionSelect =this->fullScreenImage.copy(snapRect);
    ImgRegionSelect.save(filename);
    close();
}
void ScreenShot::copy()
{
    QPixmap ImgRegionSelect = this->fullScreenImage.copy(snapRect);
    qApp->clipboard ()->setPixmap (ImgRegionSelect);
   this->close();
   this->releaseMouse ();
    //qApp->clipboard ()->setText ("abcdd");
}
void ScreenShot::reSelect ()
{
    draw=DrawInit;
    startPoint=QPoint();
    endPoint=QPoint();
    update();
}

void ScreenShot::contextMenuEvent(QContextMenuEvent *event)
{
	QAction *actionClose = new QAction(tr("退出  &Q"),this);
	QAction *actionSave;
    QAction * actionCopy;
	QMenu menu(this);
	if (snapRect.contains(event->pos()))
	{ 
		actionSave = new QAction(tr("保存为  &S"), this);
		menu.addAction(actionSave);
		connect(actionSave, SIGNAL(triggered()), this, SLOT(save()));
        actionCopy = new QAction(tr("重选"),this);
        menu.addAction (actionCopy);
        connect(actionCopy,SIGNAL(triggered()),this,SLOT(reSelect()));
	}
	menu.addAction(actionClose);
	connect(actionClose, SIGNAL(triggered()), this, SLOT(close()));
	menu.exec(event->pos());
}
